package com.pachong.android.framework.picture.browser.view;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.f2prateek.dart.Dart;
import com.f2prateek.dart.InjectExtra;
import com.f2prateek.dart.Nullable;
import com.pachong.android.framework.R;
import com.pachong.android.framework.picture.browser.constant.Constant;

import java.util.ArrayList;

/**
 * 两个静态方法，通过传activity,url或者url容器,以及一个action码
 * 在activity的回调中使用
 * {
 * Constant.CALLBACK_CODE_SELETE
 * Constant.CALLBACK_CODE_DELECT
 * Constant.CALLBACK_CODE_SAVE    获得回调的url容器
 * }
 * Created by Zoi.
 * E-mail：KyluZoi@gmail.com
 * 2016/6/8
 */

public class PictureBrowserActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener, DelectSelectDialog.OnSelectLayout
        , PictureFragment.SaveImpl, View.OnClickListener {

    //获取列表码与请求码
    static final String PHOTO_LIST = "PHOTO_LIST";

    @InjectExtra(Constant.PhotoBroAction.ACTION_KEY)
    String actionKey;
    @InjectExtra(PHOTO_LIST)
    ArrayList<String> mdatas;
    @Nullable
    @InjectExtra("fileurl")
    String mSaveUrl = "";

    TextView btnSelectComplete;
    RelativeLayout icBottomWatch;
    ThrowViewpager vpPhotoPager;
    TextView btnTitleBack;
    TextView tvTitleNum;
    ImageView btnTitleRight;
    TextView tvSelectNum;

    private boolean mNumIsShow = false;
    private boolean mIsSelected = false;
    private ArrayList<Fragment> mFragments = new ArrayList<Fragment>();
    private PagerAdapter mAdapter;

    private int mPosition = 0;

    private String mSelectNum = "0";

    private ArrayList<String> mTempData;
    private DelectSelectDialog mDialog;

    public static void watchPhoto(Activity activity, String url, String actionKey) {
        PictureBrowserActivity.watchPhoto(activity, url, actionKey, null);
    }

    public static void watchPhoto(Activity activity, String url, String actionKey, String filePath) {
        ArrayList<String> datas = new ArrayList<>();
        datas.add(url);
        PictureBrowserActivity.watchPhoto(activity, datas, actionKey, filePath);
    }

    public static void watchPhoto(Activity activity, ArrayList<String> photoList, String actionKey, String filePath) {
        Intent intent = new Intent(activity, PictureBrowserActivity.class);
        intent.putExtra(PHOTO_LIST, photoList);
        intent.putExtra(Constant.PhotoBroAction.ACTION_KEY, actionKey);
        if (!TextUtils.isEmpty(filePath) && filePath.length() > 0) {
            intent.putExtra("fileurl", filePath);
        }
        activity.startActivityForResult(intent, Constant.REQUEST_CODE);

    }

    public static void watchPhoto(Activity activity, ArrayList<String> photoList, String actionKey) {
        watchPhoto(activity, photoList, actionKey, null);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_picture_browser);

        btnSelectComplete = (TextView) findViewById(R.id.btn_select_complete);
        btnSelectComplete.setOnClickListener(this);
        icBottomWatch = (RelativeLayout) findViewById(R.id.ic_bottom_watch);
        icBottomWatch.setOnClickListener(this);
        vpPhotoPager = (ThrowViewpager) findViewById(R.id.vp_photo_pager);
        btnTitleBack = (TextView) findViewById(R.id.btn_title_back);
        btnTitleBack.setOnClickListener(this);
        tvTitleNum = (TextView) findViewById(R.id.tv_title_num);
        tvTitleNum.setOnClickListener(this);
        btnTitleRight = (ImageView) findViewById(R.id.btn_title_right);
        btnTitleRight.setOnClickListener(this);
        tvSelectNum = (TextView) findViewById(R.id.tv_select_num);

        initGetIntent();
        initView();
        mDialog = DelectSelectDialog.newInstance(getString(R.string.make_sure_delete), getString(R.string.delete), getString(R.string.cancel));
        mDialog.setOnSelectLayout(this);
        tvSelectNum.setText(mSelectNum + "");

    }


    private void initGetIntent() {
//        action = (String) getIntent().getExtras().get("actionkey");
//        mdatas=getIntent().getStringArrayListExtra(PHOTO_LIST);
        Dart.inject(this);
        if (mSaveUrl == null || mSaveUrl.equals("") || mSaveUrl == "") {
            mSaveUrl = Constant.DEFAULT_FILE_PATH;
        }

    }


    private void initView() {
        mTempData = new ArrayList<>();
        //通过attion判断选择界面
        switch (actionKey) {
            case Constant.RESULT_CODE_DELETE:
                btnTitleRight.setBackgroundResource(R.mipmap.btn_delete);
                break;
            case Constant.RESULT_CODE_SAVE:
                mNumIsShow = true;
                btnTitleRight.setBackgroundResource(R.mipmap.btn_save);
                break;
            case Constant.RESULT_CODE_SELETE:
                mNumIsShow = true;
                btnTitleRight.setBackgroundResource(R.mipmap.btn_correct);
                icBottomWatch.setVisibility(View.VISIBLE);
                break;
        }
        //通过data.size来创建多个fragment
        for (int i = 0; i < mdatas.size(); i++) {
            PictureFragment fragment = PictureFragment.newInstance(mdatas.get(i), mSaveUrl);
            fragment.setImp(this);
            mFragments.add(fragment);
        }
        mAdapter = new PagerAdapter(getSupportFragmentManager(), mFragments);
        vpPhotoPager.setAdapter(mAdapter);
        vpPhotoPager.setOnPageChangeListener(this);

    }

    @Override
    public void onBackPressed() {
        if (Constant.RESULT_CODE_SAVE.equals(actionKey)) {
            Intent ii = new Intent();
            ii.putStringArrayListExtra(Constant.CALLBACK_DATA_CODE, mTempData);
            this.setResult(Constant.CALLBACK_CODE_SAVE, ii);
        }
        super.onBackPressed();
    }


    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        int i = view.getId();
        if (i == R.id.btn_title_back) {
            switch (actionKey) {
                case Constant.RESULT_CODE_SAVE:
                    Intent ii = new Intent();
                    ii.putStringArrayListExtra(Constant.CALLBACK_DATA_CODE, mTempData);
                    this.setResult(Constant.CALLBACK_CODE_SAVE, ii);
            }
            this.finish();

        } else if (i == R.id.btn_select_complete) {
            intent.putStringArrayListExtra(Constant.CALLBACK_DATA_CODE, mTempData);
            this.setResult(Constant.CALLBACK_CODE_SELETE, intent);
            this.finish();

        } else if (i == R.id.tv_title_num) {// TODO: 2016/6/9 预留中间点击事件

        } else if (i == R.id.btn_title_right) {
            Log.d("PictureBrowserActivity", actionKey);
            Log.d("PictureBrowserActivity", "Constant.CALLBACK_CODE_SELETE:" + Constant.CALLBACK_CODE_SELETE);
            switch (actionKey) {
                case Constant.RESULT_CODE_DELETE:
                    mDialog.show(getSupportFragmentManager(), "");
//                        this.finish();
                    break;
                case Constant.RESULT_CODE_SAVE:
//                        intent.putExtra(Constant.CALLBACK_DATA_CODE, mdatas.get(mPosition));
//                        this.setResult(Constant.CALLBACK_CODE_SAVE, intent);
                    PictureFragment photoFragment = (PictureFragment) mFragments.get(mPosition);
                    photoFragment.saveBitmap();
//                        this.finish();
                    break;
                case Constant.RESULT_CODE_SELETE:
                    if (mIsSelected) {
                        btnTitleRight.setBackgroundResource(R.mipmap.btn_correct);
                        mTempData.remove(mdatas.get(mPosition));
                        mIsSelected = !mIsSelected;
                    } else {
                        btnTitleRight.setBackgroundResource(R.mipmap.btn_correct_normal);
                        mTempData.add(mdatas.get(mPosition));
                        mIsSelected = !mIsSelected;
                    }
                    Log.d("DemoActivity", "mSelectNum:" + mSelectNum);
                    mSelectNum = mTempData.size() + "";
                    tvSelectNum.setText(mSelectNum);
                    break;
            }

        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (mNumIsShow) {
            tvTitleNum.setText(getString(R.string.title_num, position + 1, mdatas.size()));
        }
        if (actionKey.equals(Constant.RESULT_CODE_SELETE)) {
            if (mTempData.contains(mdatas.get(position))) {
                btnTitleRight.setBackgroundResource(R.mipmap.btn_correct_normal);
                mIsSelected = true;
            } else {
                btnTitleRight.setBackgroundResource(R.mipmap.btn_correct);
                mIsSelected = false;
            }
        }
        mPosition = position;
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClickFirst(String str1, String tag) {

    }

    @Override
    public void onClickSecond(String str2, String tag) {

        mTempData.add(mdatas.get(mPosition));
        Intent intent = new Intent();
        intent.putExtra(Constant.CALLBACK_DATA_CODE, mTempData);
        this.setResult(Constant.CALLBACK_CODE_DELECT, intent);
        Log.d("PictureBrowserActivity", "mPosition:" + mPosition);
        mAdapter.remove(mPosition);
        mdatas.remove(mPosition);
        Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClickThird(String tag) {
        mDialog.dismiss();
    }

    @Override
    public void saveFileSuccess(String filePath) {
        mTempData.add(filePath);
    }


    /**
     * viewPager适配器 内部类
     */
    private class PagerAdapter extends FragmentStatePagerAdapter {
        private ArrayList<Fragment> fragments = new ArrayList<Fragment>();

        public PagerAdapter(FragmentManager fragmentManager,
                            ArrayList<Fragment> fragments) {
            super(fragmentManager);
            this.fragments = fragments;
            notifyDataSetChanged();
        }


        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public void notifyDataSetChanged() {
            super.notifyDataSetChanged();
        }

        @Override
        public int getItemPosition(Object object) {
            if (getCount() > 0) {
                return POSITION_NONE;
            }
            PictureBrowserActivity.this.finish();
            return super.getItemPosition(object);
        }


        public void remove(int pos) {
            fragments.remove(pos);
            notifyDataSetChanged();
        }
    }

}

